#include "Scene.h"
#include "../shapes/BasicShape.h"
#include <qdebug.h>
#include <windows.h>

Scene::Scene() {
}

Scene::~Scene() {

}

void Scene::addShape(Shape* shape)
{
	this->meshs->insert(shape);
}

GLfloat* Scene::createVertex()
{
	this->vertexSize = 0;
	QSet<Shape*>::const_iterator i;
	for (i = this->meshs->constBegin(); i != this->meshs->constEnd(); ++i) {
		Shape* shape = (*i);
		if (static_cast<RectangleShape*>(shape)) {
			this->vertexSize += 12;
		}
	}
	delete this->vertex;
	this->vertex = new GLfloat[this->vertexSize];
	int index = 0;
	for (i = this->meshs->constBegin(); i != this->meshs->constEnd(); ++i) {
		Shape* shape = (*i);
		int currentIndex = 0;
		if (dynamic_cast<RectangleShape*>(shape)) {
			RectangleShape* rectangle = (RectangleShape*)shape;
			GLfloat* currentVertex = rectangle->getVertex();
			float halfWidth = rectangle->getWidth() / 2;
			float halfHeight = rectangle->getHeight() / 2;
			float sceneHalfWidth = 1;
			currentVertex[currentIndex++] = halfWidth;
			currentVertex[currentIndex++] = halfHeight;
			currentVertex[currentIndex++] = -halfWidth;
			currentVertex[currentIndex++] = +halfHeight;
			currentVertex[currentIndex++] = -halfWidth;
			currentVertex[currentIndex++] = -halfHeight;
			currentVertex[currentIndex++] = -halfWidth;
			currentVertex[currentIndex++] = -halfHeight;
			currentVertex[currentIndex++] = +halfWidth;
			currentVertex[currentIndex++] = -halfHeight;
			currentVertex[currentIndex++] = +halfWidth;
			currentVertex[currentIndex++] = +halfHeight;
		}
		else if (dynamic_cast<CircleShape*>(shape)) {
			CircleShape* circleShape = (CircleShape*)shape;
			GLfloat* currentVertex = new GLfloat[(36 - 3) * 3 * 2 + 3 * 2];
			GLfloat* tempVertex = new GLfloat[36 * 2];

			circleShape->setVertex(currentVertex);
			circleShape->setVertexSize((36 - 3) * 3 * 2 + 3 * 2);
			int radius = circleShape->getRadius();
			int currentIndex = 0;
			float dealtaAngle = -M_PI * 10 / 180;
			for (int i = 0;i < 36;i++) {
				tempVertex[currentIndex++] = std::cos(dealtaAngle * i) * radius;
				tempVertex[currentIndex++] = std::sin(dealtaAngle * i) * radius;
			}
			currentIndex = 0;
			for (int i = 0;i < 36;i++) {
				currentVertex[currentIndex++] = tempVertex[(i * 2)];
				currentVertex[currentIndex++] = tempVertex[(i * 2) + 1];
				if (i > 2) {
					currentVertex[currentIndex++] = tempVertex[(i - 1) * 2];
					currentVertex[currentIndex++] = tempVertex[(i - 1) * 2 + 1];
					currentVertex[currentIndex++] = tempVertex[0];
					currentVertex[currentIndex++] = tempVertex[1];
				}
			}
		}
		else {
		}
	}
	return vertex;
}

void Scene::reszie(int w, int h) {
	this->width = w;
	this->height = h;
}

